package com.example.recyclerviewtest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

public class GridLayout extends AppCompatActivity {

    private List<Fruit> fruitList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid_layout_manager);

        intiFruits();
        RecyclerView recyclerView=findViewById(R.id.recycler_view2);
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);

        //设置布局排列方向，默认纵向，此处修改为横向
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);

        recyclerView.setLayoutManager(layoutManager);
        FruitAdapter_GridLayout adapter=new FruitAdapter_GridLayout(fruitList);
        recyclerView.setAdapter(adapter);
    }

    //初始化水果数据
    private void intiFruits(){
        for(int i=0;i<10;i++){
            Fruit apple=new Fruit("Apple"+i,R.drawable.apple_pic);
            fruitList.add(apple);
            Fruit banana=new Fruit("Banana"+i,R.drawable.banana_pic);
            fruitList.add(banana);
        }
    }
}
